<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>PL/0编译器</title>
</head>

<body>
  <script src="../js/vue.js"></script>
  <script src="../element-ui/lib/index.js"></script>
  <link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
  <script src="../js/axios-0.18.0.js"></script>

  <div id="app">
    <el-container>
      <el-col :span="3">
        <div>&nbsp;</div>
      </el-col>
      <el-aside width="210px">
        <el-menu default-active="1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
          background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" :default-openeds="['1', '2']">
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-folder"></i>
              <span>常规测试用例</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="1-1" @click="setCode1">column</el-menu-item>
              <el-menu-item index="1-2" @click="setCode2">daffodilnum</el-menu-item>
              <el-menu-item index="1-3" @click="setCode3">F2C</el-menu-item>
              <el-menu-item index="1-4" @click="setCode4">function</el-menu-item>
              <el-menu-item index="1-5" @click="setCode5">max</el-menu-item>
              <el-menu-item index="1-6" @click="setCode6">permutation</el-menu-item>
              <el-menu-item index="1-7" @click="setCode7">quicksort</el-menu-item>
              <el-menu-item index="1-8" @click="setCode8">sample0</el-menu-item>
              <el-menu-item index="1-9" @click="setCode9">sample1</el-menu-item>
              <el-menu-item index="1-10" @click="setCode10">sample2</el-menu-item>
              <el-menu-item index="1-11" @click="setCode11">sample3</el-menu-item>
              <el-menu-item index="1-12" @click="setCode12">SumOfn!</el-menu-item>
              <el-menu-item index="1-13" @click="setCode13">test_recursive</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-folder-opened"></i>
              <span>其他测试用例</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="1-1">选项1</el-menu-item>
              <el-menu-item index="1-2">选项2</el-menu-item>
              <el-menu-item index="1-3">选项3</el-menu-item>
              <el-menu-item index="1-4">选项4</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="20">
              <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect"
                background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
                <el-menu-item index="1" @click="click1"> 个 人 中 心 </el-menu-item>
                <el-menu-item index="2" @click="click2"> 词 法 分 析 </el-menu-item>
                <el-menu-item index="3" @click="click3"> 语 法 分 析 </el-menu-item>
              </el-menu>
            </el-col>
          </el-row>
        </el-header>
        <el-main>
          <el-row>
            <el-col :span="1">
              <div>&nbsp;</div>
            </el-col>
            <el-col :span="8">
              <el-input type="textarea" :rows="26" placeholder="请输入PL/0代码" v-model="textarea1"></el-input>
            </el-col>
            <el-col :span="1">
              <div>&nbsp;</div>
            </el-col>
            <el-col :span="8">
              <el-input type="textarea" :rows="26" placeholder="代码分析区" v-model="textarea2" :disabled="false"></el-input>
            </el-col>
          </el-row>

          <el-row>
            <el-col>&nbsp;</el-col>
          </el-row>

          <el-row>
            <el-col :span="3">
              <div>&nbsp;</div>
            </el-col>
            <el-col :span="11">
              <el-upload class="upload-demo" action="" :on-preview="handlePreview" :on-remove="handleRemove"
                :before-remove="beforeRemove" multiple :limit="3" :on-exceed="handleExceed" :file-list="fileList"
                :on-change="uploadTxt">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-col>
            <el-col :span="4">
              <el-button size="small" type="success" @click="analyze">词法分析</el-button>
            </el-col>
          </el-row>
        </el-main>
        </el-container>
    </el-container>
  </div>

  <script>
    new Vue({
      el: "#app",

      mounted() {
        this.getAccount();
      },

      methods: {
        getAccount() {
          let url = location.search; //获取url中"?"符后的字串
          if (url.indexOf("?") != -1) {    //判断是否有参数
            // 从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
            let str = url.substr(1).split("=");   //用等号进行分隔
            this.pl0_code.account = str[1];
          }
          else {
            this.$message.error("错误！非法访问");
            location.replace("http://localhost:8080/home.html");
          }
        },
        analyze() {
          this.pl0_code.code = this.textarea1;
          let date = new Date();
          let year = date.getFullYear();        //年 ,从 Date 对象以四位数字返回年份
          let month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
          let day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
          let hours = date.getHours();          //小时 ,返回 Date 对象的小时 (0 ~ 23)
          let minutes = date.getMinutes();      //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
          let seconds = date.getSeconds();      //秒 ,返回 Date 对象的秒数 (0 ~ 59)
          //修改月份格式
          if (month >= 1 && month <= 9) {
            month = "0" + month;
          }
          //修改日期格式
          if (day >= 0 && day <= 9) {
            day = "0" + day;
          }
          //修改小时格式
          if (hours >= 0 && hours <= 9) {
            hours = "0" + hours;
          }
          //修改分钟格式
          if (minutes >= 0 && minutes <= 9) {
            minutes = "0" + minutes;
          }
          //修改秒格式
          if (seconds >= 0 && seconds <= 9) {
            seconds = "0" + seconds;
          }
          //获取当前系统时间  格式(yyyy-mm-dd hh:mm:ss)
          let currentFormatDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
          this.pl0_code.time = currentFormatDate;
          axios.post("http://localhost:8080/word", this.pl0_code).then(resp => {
            if (resp.data.code === 20011) {
              this.$message.success("分析成功！");
              this.myPair = resp.data.data;
              for (let i = 0; i < this.myPair.length; i++) {
                switch (this.myPair[i].first) {
                  case 1: this.textarea2 += (i + 1) + "、标识符："; break;
                  case 2: this.textarea2 += (i + 1) + "、常数：  "; break;
                  case 3: this.textarea2 += (i + 1) + "、关键字："; break;
                  case 4: this.textarea2 += (i + 1) + "、运算符："; break;
                  case 5: this.textarea2 += (i + 1) + "、界符：  "; break;
                  case 6: this.textarea2 += (i + 1) + "、错误：  "; break;
                  case 7: this.textarea2 += (i + 1) + "、结束：  "; break;
                }
                this.textarea2 += this.myPair[i].second + "\n";
              }
            }
            else if (resp.data.code === 20010) {
              this.$message.error("分析失败！");
            }
            else {
              this.$message.error("服务器请求超时！");
            }
          });
        },
        uploadTxt(file, fileList) {
          this.textarea1 = "";
          this.textarea2 = "";
          let reader = new FileReader();
          reader.readAsText(file.raw);
          reader.onload = () => {
            this.textarea1 = reader.result;
          };
        },
        setCode1() {
          this.textarea1 =
            "var r, h, len, a1, a2, volumn;\n" +
            "\n" +
            "begin\n" +
            "    read(r);\n" +
            "    read(h);\n" +
            "\n" +
            "    len:= 2 * 3 * r;\n" +
            "    a1:= 3 * r * r;\n" +
            "    a2:= a1 + a1 + len * h;\n" +
            "    volumn:= a1 * h;\n" +
            "\n" +
            "    write(len);\n" +
            "    write(a1);\n" +
            "    write(a2);\n" +
            "    write(volumn);\n" +
            "end.";
          this.textarea2 = "";
        },
        setCode2() {
          this.textarea1 =
            "var n, m, i, j, digit(1:3);\n" +
            "\n" +
            "procedure init;\n" +
            "begin\n" +
            "    i := 1;\n" +
            "    while i < 4 do\n" +
            "        begin\n" +
            "            digit(i) := 0;\n" +
            "            i := i + 1\n" +
            "        end\n" +
            "end;\n" +
            "\n" +
            "procedure parse;\n" +
            "var tmp;\n" +
            "begin\n" +
            "  tmp := n;\n" +
            "\ti := 0;\n" +
            "\twhile n # 0 do\n" +
            "\t    begin\n" +
            "\t        m := n / 10;\n" +
            "          i := i + 1;\n" +
            "          digit(i) := n - m * 10;\n" +
            "\t        n := m;\n" +
            "\t    end;\n" +
            "  n := tmp\n" +
            "end;\n" +
            "\n" +
            "procedure sumdigit;\n" +
            "begin\n" +
            "    m := digit(1) * digit(1) * digit(1) +\n" +
            "        digit(2) * digit(2) * digit(2) +\n" +
            "        digit(3) * digit(3) * digit(3)\n" +
            "end;\n" +
            "\n" +
            "begin\n" +
            "    n := 1;\n" +
            "    while n < 1000 do\n" +
            "        begin\n" +
            "            call init;\n" +
            "            call parse;\n" +
            "            call sumdigit;\n" +
            "            if m = n then\n" +
            "                write(n);\n" +
            "            n := n + 1\n" +
            "        end\n" +
            "end.";
          this.textarea2 = "";
        },
        setCode3() {
          this.textarea1 =
            "var f, c;\n" +
            "\n" +
            "begin\n" +
            "    read(f);\n" +
            "    c := 5 * (f - 32) / 9;\n" +
            "    write(c)\n" +
            "end.";
          this.textarea2 = "";
        },
        setCode4() {
          this.textarea1 =
            "var x, y;\n" +
            "\n" +
            "procedure function;\n" +
            "if x < 1 then\n" +
            "    y := x\n" +
            "else\n" +
            "    if x < 10 then\n" +
            "        y := 2 * x - 1\n" +
            "    else\n" +
            "        y := 3 * x - 11\n" +
            ";\n" +
            "begin\n" +
            "    read(x);\n" +
            "    call function;\n" +
            "    write(y)\n" +
            "end.";
          this.textarea2 = "";
        },
        setCode5() {
          this.textarea1 =
            "var a, b, c, max;\n" +
            "\n" +
            "procedure p;\n" +
            "begin\n" +
            "    read(a);\n" +
            "    read(b);\n" +
            "    read(c);\n" +
            "    if a > b\n" +
            "    then max := a\n" +
            "    else max := b;\n" +
            "    \n" +
            "    if max > c\n" +
            "    then max := max\n" +
            "    else max := c;\n" +
            "\n" +
            "    write(max)\n" +
            "end;\n" +
            "\n" +
            "call p.";
          this.textarea2 = "";
        },
        setCode6() {
          this.textarea1 =
            "const m = 4;\n" +
            "var   a(1:m), ended, n;\n" +
            "\n" +
            "procedure next;\n" +
            "var i;\n" +
            "        procedure cycle;\n" +
            "        var t;\n" +
            "        begin\n" +
            "            t := a(i);\n" +
            "            while i < m do begin\n" +
            "                a(i) := a(i+1);\n" +
            "                i := i + 1;\n" +
            "                end;\n" +
            "            a(m) := t;\n" +
            "        end;\n" +
            "\n" +
            "begin\n" +
            "    if n > 1 then begin\n" +
            "        if a(m-n+1) = n then begin\n" +
            "            n := n - 1;\n" +
            "            call next;\n" +
            "            n := n + 1;\n" +
            "        end;\n" +
            "\tif ended = 0 then begin\n" +
            "            i := m-n+1;\n" +
            "            call cycle;\n" +
            "        end;\n" +
            "    end\n" +
            "    else\n" +
            "        ended := 1;\n" +
            "end;\n" +
            "\n" +
            "begin\n" +
            "    a(1) := 1;    a(2) := 2;    a(3) := 3;    a(4) := 4;\n" +
            "    ended := 0;\n" +
            "\n" +
            "    while ended = 0 do begin\n" +
            "    \twrite (a(1), a(2), a(3), a(4));\n" +
            "        n := m;\n" +
            "        call next;\n" +
            "    end;\n" +
            "end.";
          this.textarea2 = "";
        },
        setCode7() {
          this.textarea1 =
            "const\n"
            + "\t\tn\t= 10;\n"
            + "\tvar\n"
            + "\t\ta(1:n), sp, stack(1:100), left, right, seed;\n"
            + "\n"
            + "\tprocedure printarray;\n"
            + "\tvar i;\n"
            + "\tbegin\n"
            + "\t\ti:= 1;\n"
            + "\t\twhile i <= n do \tbegin\n"
            + "\t\t\twrite(a(i)); \n"
            + "\t\t\ti:= i + 1 \n"
            + "\t\tend;\n"
            + "\t\t\n"
            + "\tend;\n"
            + "\n"
            + "\tprocedure initialize;\n"
            + "\tvar i, k;\n"
            + "\tbegin\n"
            + "\t\ti:= 1;\n"
            + "                k := -1;\n"
            + "                read (seed);\n"
            + "                while seed < -512 do\n"
            + "                   seed := seed/2;\n"
            + "                while seed > 512 do\n"
            + "                   seed := seed/2;\n"
            + "\t\twhile i <= n do begin\n"
            + "\t                seed := ((seed + 30) - (seed + 30)/64 * 64)  * k;\n"
            + "                        k:= -k;\n"
            + "\t\t\ta(i):= i + seed;\n"
            + "\t\t\ti:= i + 1;\n"
            + "\t\tend;\n"
            + "\tend;\n"
            + "\n"
            + "\tprocedure quicksort;\n"
            + "\tvar i, j, x, w, middle;\n"
            + "\tbegin\n"
            + "\t\ti:= left; j:= right;\n"
            + "\t\tmiddle:= (left + right)/2;\n"
            + "\t\tx:= a(middle);\n"
            + "\t\twhile i < j do\n"
            + "\t\tbegin\n"
            + "\t\t\twhile a(i) < x do i:= i + 1;\n"
            + "\t\t\twhile a(j) > x do j:= j - 1;\n"
            + "\t\t\tif i <= j then begin\n"
            + "\t\t\t\tw:= a(i); a(i):= a(j); a(j):= w;\n"
            + "\t\t\t\ti:= i + 1;\n"
            + "\t\t\t\tj:= j - 1;\n"
            + "\t\t\tend\n"
            + "\t\tend;\n"
            + "\t\t\n"
            + "\t\t\n"
            + "\t\tif left < j then begin\n"
            + "                    sp := sp + 1;\n"
            + "                    stack(sp) := right;\n"
            + "                    right := j;\n"
            + "                    call quicksort;\n"
            + "                    right := stack(sp);\n"
            + "                    sp := sp - 1;\n"
            + "                end;\n"
            + "\t\tif i < right then begin\n"
            + "                    sp := sp + 1;\n"
            + "                    stack(sp) := left;\n"
            + "                    left := i;\n"
            + "                    call quicksort;\n"
            + "                    left := stack(sp);\n"
            + "                    sp := sp - 1;\n"
            + "                end;\n"
            + "\tend;\n"
            + "\n"
            + "begin\n"
            + "\tcall initialize;\n"
            + "\tcall printarray;\n"
            + "\twrite (0, 0, 0, 0, 0, 0, 0, 0);\n"
            + "\n"
            + "        sp := 0;\n"
            + "        left := 1;     right := n;\n"
            + "\tcall quicksort;\n"
            + "\tcall printarray;\n"
            + "end.";
          this.textarea2 = "";
        },
        setCode8() {
          this.textarea1 =
            "const a=10; var result;\n"
            + "procedure average;\n"
            + "var temp, addition, count;\n"
            + "\n"
            + "procedure add;\n"
            + "  begin\n"
            + "     temp:=temp+2;\n"
            + "     write(temp);\n"
            + "  end;\n"
            + "\n"
            + "begin\n"
            + "    temp:=a;\n"
            + "    count:=1;\n"
            + "    addition:=temp;\n"
            + "    while temp<=20 do\n"
            + "       begin\n"
            + "           call add;\n"
            + "           count:=count+1;\n"
            + "           addition:=addition+temp;\n"
            + "       end;\n"
            + "    result:=addition/count;\n"
            + "end;\n"
            + "\n"
            + "begin\n"
            + "   write(a);\n"
            + "   call average;\n"
            + "   write(result);\n"
            + "end.";
          this.textarea2 = "";
        },
        setCode9() {
          this.textarea1 =
            "const true=1, false=0;\n"
            + "var i, j, k;\n"
            + "procedure recttri;\n"
            + "\n"
            + "var temp1, temp2, temp3;\n"
            + "    begin\n"
            + "         write ( i );\n"
            + "         write (j);\n"
            + "         write(k);\n"
            + "         temp2:=j*j;\n"
            + "         temp3:=k*k;\n"
            + "         while temp1+temp2 #temp3 do\n"
            + "                begin\n"
            + "                     write(false);\n"
            + "                      i:=i+1;\n"
            + "\t\t      temp1:=i*i;\n"
            + "                end;\n"
            + "                write(true);\n"
            + "        end;\n"
            + "\n"
            + "begin\n"
            + "i:=3;\n"
            + "j:=8;\n"
            + "k:=10;\n"
            + "call recttri;\n"
            + "write(i);\n"
            + "end.";
          this.textarea2 = "";
        },
        setCode10() {
          this.textarea1 =
            "const pi=3;\n"
            + "var c, flag;\n"
            + "procedure compare;\n"
            + "var area1, area2;\n"
            + "procedure rad;\n"
            + "var r1, r2;\n"
            + "\n"
            + "begin\n"
            + "r1:=c/(2*pi);\n"
            + "r2:=c/4;\n"
            + "area1:=pi*r1*r1;\n"
            + "area2:=r2*r2;\n"
            + "end;\n"
            + "\n"
            + "begin\n"
            + "call rad;\n"
            + "if area1<area2 then\n"
            + "begin\n"
            + "write (area2);\n"
            + "flag:=2;\n"
            + "end;\n"
            + "end;\n"
            + "\n"
            + "begin\n"
            + "flag:=1;\n"
            + "c:=16;\n"
            + "call compare;\n"
            + "write(flag);\n"
            + "end.";
          this.textarea2 = "";
        },
        setCode11() {
          this.textarea1 =
            "const bound=30;\n"
            + "var input, count;\n"
            + "procedure guess;\n"
            + "var temp, upper, lower;\n"
            + "procedure locate;\n"
            + "\n"
            + "begin\n"
            + "temp:=(upper+lower)/2;\n"
            + "if temp<input then\n"
            + "lower:=temp+1;\n"
            + "if temp>input then\n"
            + "upper:=temp-1;\n"
            + "end;\n"
            + "\n"
            + "begin\n"
            + "upper:=bound;\n"
            + "lower:=0;\n"
            + "temp:=(upper+lower)/2;\n"
            + "while temp#input do\n"
            + "begin\n"
            + "count:=count+1;\n"
            + "call locate;\n"
            + "end;\n"
            + "end;\n"
            + "\n"
            + "begin\n"
            + "input:=6;\n"
            + "count:=1;\n"
            + "call guess;\n"
            + "write(count);\n"
            + "end.";
          this.textarea2 = "";
        },
        setCode12() {
          this.textarea1 =
            "var n, m, fact, sum;\n"
            + "\n"
            + "procedure factorial;\n"
            + "begin\n"
            + "    if m > 0 then\n"
            + "        begin\n"
            + "            fact := fact * m;\n"
            + "            m := m - 1;\n"
            + "            call factorial\n"
            + "        end;\n"
            + "end;\n"
            + "\n"
            + "begin\n"
            + "    read(n);\n"
            + "    sum := 0;\n"
            + "    while n > 0 do\n"
            + "        begin\n"
            + "            m := n;\n"
            + "            fact := 1;\n"
            + "            call factorial;\n"
            + "            sum  := sum + fact;\n"
            + "            n := n - 1\n"
            + "        end;\n"
            + "    write(sum)\n"
            + "end.";
          this.textarea2 = "";
        },
        setCode13() {
          this.textarea1 =
            "var n;\n"
            + "\n"
            + "procedure rec;\n"
            + "begin\n"
            + "    if n # 0 then\n"
            + "        begin\n"
            + "            write(n);\n"
            + "            n := n - 1;\n"
            + "            call rec\n"
            + "        end;\n"
            + "end;\n"
            + "\n"
            + "begin\n"
            + "    read(n);\n"
            + "    call rec\n"
            + "end.";
          this.textarea2 = "";
        },
        click1() {
          location.replace("http://localhost:8080/user.html?account=" + this.pl0_code.account);
        },
        click2() {
          location.replace("http://localhost:8080/word.html?account=" + this.pl0_code.account);
        },
        click3() {
          location.replace("http://localhost:8080/phrase.html?account=" + this.pl0_code.account)
        },
        handleSelect(key, keyPath) { },
        handleRemove(file, fileList) { },
        handlePreview(file) { },
        handleExceed(files, fileList) {
          this.$message.error(`只能上传一个文件`);
        },
        beforeRemove(file, fileList) {
          return this.$confirm("确定移除 ${ file.name }？");
        }
      },

      data() {
        return {
          textarea1: "",
          textarea2: "",
          activeIndex: "2",
          fileList: [],
          pl0_code: {
            id: "",
            account: "",
            code: "",
            time: ""
          },
          myPair: []
        }
      }
    })
  </script>
</body>

</html>